const Request = require("../../utils/request");
const Pageshare = require("../../utils/pageshare");

Page({

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return Pageshare.baseshare({
      title: "用户中心",
      params: {
        pageId: 5,
        source: "用户中心"
      }
    });
  },

  /**
   * 页面的初始数据
   */
  data: {
    msgnum: 0,
    user: {},
    visible: false,
    status: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //todo 应该使用app.js下统一的接口
    let self = this;
    Request.get("/user/info").then(function(res) {
      if (res.data.register_status === -1) {
        self.setData({
          status: "不通过"
        });
        wx.showModal({
          title: "提示",
          content: "审核不通过，请修改注册信息",
          showCancel: false,
          success: function(res) {
            wx.navigateTo({
              url: "/pages/user/supplement/supplement"
            });
          }
        })
        return;
      } else if (res.data.register_status === 0) {
        self.setData({
          status: "未审核"
        });
      } else if (res.data.register_status === 1) {
        //self.setData({ status: "通过" });
      } else {
        self.setData({
          status: "未定义"
        });
      }
      let _user = {
        id: res.data.id,
        username: res.data.username,
        nickname: res.data.nickname,
        city: res.data.city,
        company: res.data.company,
        country: res.data.country,
        created_time: res.data.created_time,
        email: res.data.email,
        headimgurl: res.data.headimgurl,
        is_registered: res.data.is_registered,
        register_status: res.data.register_status,
        mobile: res.data.mobile,
        money: res.data.money,
        openid: res.data.openid,
        position: res.data.position,
        province: res.data.province,
        sex: res.data.sex,
        share_id: res.data.share_id,
        type: res.data.type,
        unionId: res.data.unionId
      };
      getApp().globalData.userInfo = _user;
      self.setData({
        user: _user
      });
      Request.get("/api/user").then((resp) => {
        getApp().globalData.msgnum = resp.data.newMessageNum;
        self.setData({
          msgnum: resp.data.newMessageNum
        });
        if (resp.data.newMessageNum > 0) {
          wx.setTabBarBadge({
            index: 4,
            text: resp.data.newMessageNum.toString()
          });
        }
      });
    });
  },

  customservice: function(e) {
    this.setData({
      visible: true
    });
  }
})